
# --- SYSTEM CONFIGURATION -----------------------------------------------------

# Setup compilation and associated flags

COMPILER  = g++

CFLAGS    = -Wall -g
LFLAGS    =

# --- GLOBAL -------------------------------------------------------------------

# Create the list of 'projects' to build

PROJECT_DIRS	= $(shell find . -type d | egrep -v './[^/]*/' | egrep '\.[^.]+' | grep -v svn )
PROJECT_TARG	= $(patsubst %, %.building, $(PROJECT_DIRS))
PROJECT_CLEAN	= $(patsubst %, %.cleaning, $(PROJECT_DIRS))

#OLDPWD		= $(PWD)

# ... create overall compilation rules


%.building:
	@printf " Building ./$*... \n"
	@(cd $* ; ln -sf ../Makefile; make -sk all-proj)

%.cleaning:
	@printf " Building ./$*... \n"
	@(cd $* ; ln -sf ../Makefile; make -sk clean-proj)
	@(find -type l -exec rm {} +)

# Tell make that all and clean are not real targets
.PHONY: all clean

all: $(PROJECT_TARG)

clean: $(PROJECT_CLEAN)


# --- PER PROJECT---------------------------------------------------------------

# Setup per 'project' location

INCLD_DIRS  =
BUILD_DIRS  =
LIBS_DIRS   = 
LIBS_FILES  =

# ... include per 'project' configuration

-include Makefile.conf

# ... create the list of objects/header files

UT_DIRS		= $(shell find . -type d | egrep '/ut/' | grep -v svn)
UT_BINS		= $(patsubst %, %/ut.bin, $(UT_DIRS))

UT_TARG		= $(patsubst ./ut/%, %.target, $(UT_DIRS))


OBJ_HPP		= $(patsubst %, -I%, $(INCLD_DIRS))
OBJ_HPP_UT	= $(patsubst %, -I%, $(UT_DIRS))
OBJ_CPP		= $(patsubst %.cpp, %.o, $(foreach dir, $(BUILD_DIRS), $(wildcard $(dir)/*.cpp)))
OBJ_CPP_UT	= $(patsubst %.cpp, %.o, $(foreach dir, $(UT_DIRS), $(wildcard $(dir)/*.cpp)))
LIB_PATH	= $(patsubst %, -L%, $(LIBS_DIRS))
LIBS		= $(patsubst %, -l%, $(LIBS_FILES))

# ... setup per 'project' compilation rules

%.h:
	@true

%.hpp:
	@true

%.o: %.cpp
	@printf "Compiling $<... \n"
	$(COMPILER) $(CFLAGS) -fpic -c $(OBJ_HPP) $(OBJ_HPP_UT) $< -o $*.o

%.bin:
	@printf "  Linking $*.bin'... \n"
	$(COMPILER) $(LFLAGS) $(OBJ_CPP) $*/*.o $(LIB_PATH) $(LIBS) -o $*/ut.bin
	@true

%.target: $(OBJ_CPP) $(OBJ_CPP_UT) ut/%.bin
	@true

all-proj: $(UT_TARG)
	@printf "     DONE!\n\n"

clean-proj: print_cleaning clean-build clean-final clean-tmp
	@printf "     DONE!\n\n"

clean-build:
	rm -rf $(OBJ_CPP)
#	@printf " Cleaning 'object files'...\n"

clean-final:
	rm -rf $(UT_BINS)
#	@printf " Cleaning 'unit test executables'...\n"

clean-tmp:
	rm -rf *~
#	@printf " Cleaning 'temporary files'...\n"

# ... add some printing facilities

print_cleaning:
	@printf " Cleaning...\n"
	@true
